//
//  MyScrollViewController.m
//  StoryboardControllers
//
//  Created by Home on 2/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyScrollViewController.h"
#import "MyViewController.h"

#ifdef DEBUG
#define DebugLog(...) NSLog(@"%s (%d) %@", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:__VA_ARGS__])
#else
#define DebugLog(...)
#endif

int numberOfPages = 10;

@implementation MyScrollViewController
@synthesize Scroller;
@synthesize viewControllers;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    
    [Scroller setScrollEnabled:YES];
    Scroller.showsHorizontalScrollIndicator = NO;
    Scroller.showsVerticalScrollIndicator = NO;
    Scroller.pagingEnabled = YES;
    Scroller.delegate = self;
    
    [Scroller setContentSize:CGSizeMake((320*numberOfPages),460)];

    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
    
    viewControllers = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < numberOfPages; i++ ) {
        MyViewController *mvc = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
        mvc.Page = i+1;

        [viewControllers addObject:mvc];
        
        [mvc.view setFrame:CGRectMake((320*i),0,320,460)];
                
        
        [Scroller addSubview:mvc.view];
        
        DebugLog(@"Hello World %s, %d","Test",22);
		
    }
    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [self setScroller:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    
    
    
    CGFloat pageWidth = Scroller.frame.size.width;
    int page = floor((Scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    
    // they try and scroll left and they are on the first card, return back to the 
    // main window.
    if ( page < 0 ) {
        
        [self dismissModalViewControllerAnimated:YES];
        return;
    }
    
    // Don't scroll past the last card
    if ( page >= numberOfPages ) {
        return;
    }
    
    // When Scrolling, keep track of the current page number for other uses....
    /*
    if ( [FlashCards sharedFlashCards].currentCardNumber != (page + 1 )) {
        NSLog(@"Card number changed to %d",page+1);
        [FlashCards sharedFlashCards].currentCardNumber = page + 1;
        [ [viewControllers objectAtIndex:page] playSound:nil];
        
    }
     */
    
    
    
    
    // A possible optimization would be to unload the views+controllers which are no longer visible
}

- (void)loadScrollViewWithPage:(int)page
{
    
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
	
}

@end
